import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

class Message {
    public String from;
    public String to;
    public String message;
}

@WebServlet("/message")
public class MessageServlet extends HttpServlet {
    // 使用这个 list 保存所有消息
    // List<Message> messagesList = new ArrayList<>();

    private ObjectMapper objectMapper = new ObjectMapper();

    // 向服务器提交数据
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 把 body 中的内容解析出来称为一个 Message 对象
        Message message = objectMapper.readValue(req.getInputStream(), Message.class);
        // messagesList.add(message);
        save(message);
        // 设置状态码 200 OK
        resp.setStatus(200);
    }

    // 向服务器获取数据
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 告诉服务器, 数据是 json 格式的, 字符集为 utf8
        resp.setContentType("application/json; charset=utf8");
        List<Message> messagesList = load();
        objectMapper.writeValue(resp.getWriter(), messagesList);
        // 将 Java 对象转换为 json 字符串
        String jsonResp = objectMapper.writeValueAsString(messagesList);
        System.out.println("jsonResp:   " +jsonResp);
        resp.getWriter().write(jsonResp);
    }
    // 提供一对方法

    // 往数据库中存消息
    private void save(Message message) {
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();

            // 2. 构造 sql 语句
            String sql = "insert into message values(?, ?, ?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, message.from);
            statement.setString(2, message.to);
            statement.setString(3, message.message);

            // 3. 执行sql
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    // 从数据库中获取所有消息
    private List<Message> load() {
        List<Message> messageList = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构造 sql
            String sql = "select * from message";
            statement = connection.prepareStatement(sql);
            // 3. 执行 sql
            resultSet = statement.executeQuery();

            // 4. 遍历结果集合
            while (resultSet.next()) {
                Message message = new Message();
                message.from = resultSet.getString("from");
                message.to = resultSet.getString("to");
                message.message = resultSet.getString("message");
                messageList.add(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return messageList;
    }
}



